package top.zhangjianyong.netty.bio;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Scanner;

public class BIOClient {
    public static void main(String[] args) throws IOException {

        createClient();
    }

    public static void createClient() throws IOException {
        Socket socket = new Socket("127.0.0.1", 6666);

        OutputStream outputStream = socket.getOutputStream();
        InputStream inputStream = socket.getInputStream();
        OutputStreamWriter writer = new OutputStreamWriter(outputStream);

        new Thread(() -> {
            try {
                byte[] bytes = new byte[1024];
                while (true) {
                    int read = inputStream.read(bytes);
                    if (read != -1) {
                        System.out.println(new String(bytes));
                    } else {
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }).start();

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String next = scanner.next();
            writer.write(next);
            writer.flush();
        }

        socket.close();
    }
}
